using System;
using System.Collections.Generic;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Net;
using Microsoft.Xna.Framework.Storage;

namespace TyranntClient
{
    class MazeBlock
    {
        public int exits;
        public bool north = false;
        public bool east = false;
        public bool south = false;
        public bool west = false;
        public bool door = false;
        public Vector3 position;
        public Model floorModel;
        public Model hedgeModel;
        public Model hedgeDoorModel;
        public int blockSize;
        public bool activeBlock=false;

        public bool occupied = false;

        public MazeBlock(bool n, bool e, bool s, bool w, Vector3 pos, int cs)
        {
            exits=0;
            north = n; east = e; south = s; west = w;
            blockSize = cs;
            position = pos * blockSize;
        }

        public void Draw(Matrix viewMatrix, Matrix perspectiveMatrix, float heading)
        {
            DrawPart(viewMatrix, perspectiveMatrix, floorModel, new Vector3(position.X, position.Y, position.Z), 0, 0, "Plane");

            if (north && door) { DrawPart(viewMatrix, perspectiveMatrix, hedgeDoorModel, new Vector3(position.X, position.Y + blockSize / 2, position.Z + blockSize / 2), 0, MathHelper.Pi, "Plane"); }
            else if (!north) { DrawPart(viewMatrix, perspectiveMatrix, hedgeModel, new Vector3(position.X, position.Y + blockSize / 2, position.Z + blockSize / 2), MathHelper.Pi, MathHelper.Pi, "Plane"); }

            if (south && door) { DrawPart(viewMatrix, perspectiveMatrix, hedgeDoorModel, new Vector3(position.X, position.Y + blockSize / 2, position.Z - blockSize / 2), 0, MathHelper.Pi, "Plane"); }
            else if (!south) { DrawPart(viewMatrix, perspectiveMatrix, hedgeModel, new Vector3(position.X, position.Y + blockSize / 2, position.Z - blockSize / 2), 0, MathHelper.Pi, "Plane"); }

            if (east && door) { DrawPart(viewMatrix, perspectiveMatrix, hedgeDoorModel, new Vector3(position.X + blockSize / 2, position.Y + blockSize / 2, position.Z), MathHelper.PiOver2, MathHelper.Pi, "Plane"); }
            else if (!east) { DrawPart(viewMatrix, perspectiveMatrix, hedgeModel, new Vector3(position.X + blockSize / 2, position.Y + blockSize / 2, position.Z), MathHelper.PiOver2, MathHelper.Pi, "Plane"); }

            if (west && door) { DrawPart(viewMatrix, perspectiveMatrix, hedgeDoorModel, new Vector3(position.X - blockSize / 2, position.Y + blockSize / 2, position.Z), MathHelper.Pi + MathHelper.PiOver2, MathHelper.Pi, "Plane"); }
            else if (!west) { DrawPart(viewMatrix, perspectiveMatrix, hedgeModel, new Vector3(position.X - blockSize / 2, position.Y + blockSize / 2, position.Z), MathHelper.Pi + MathHelper.PiOver2, MathHelper.Pi, "Plane"); }
        }

        private void DrawPart(Matrix viewMatrix, Matrix perspectiveMatrix, Model model, Vector3 pos, float rotY, float rotZ, string tag)
        {
            foreach (ModelMesh mesh in model.Meshes)
            {
                if (mesh.Name == tag)
                {
                    foreach (BasicEffect effect in mesh.Effects)
                    {
                        effect.LightingEnabled = true;
                        effect.DirectionalLight0.Enabled = true;
                        effect.DirectionalLight0.DiffuseColor = new Vector3(.9f, .9f, .9f);
                        effect.DirectionalLight0.Direction = new Vector3(0.5f, -0.9f, 0.5f);
                        effect.DirectionalLight1.Enabled = true;
                        effect.DirectionalLight1.DiffuseColor = new Vector3(.6f, .6f, .3f);
                        effect.DirectionalLight1.Direction = new Vector3(-0.5f, 0.9f, -0.5f);
                        effect.FogEnabled = true;
                        effect.FogColor = new Vector3(0f, 0f, 0f);
                        effect.FogStart = blockSize*5;
                        effect.FogEnd = blockSize*15;
                        
                        effect.PreferPerPixelLighting = true;
                        //Blender coordinates are different => need to rotate around X-axis
                        effect.World = Matrix.CreateRotationX(1.5f * MathHelper.Pi) * Matrix.CreateRotationY(rotY) * Matrix.CreateRotationZ(rotZ) * Matrix.CreateTranslation(pos);
                        effect.Projection = perspectiveMatrix;
                        effect.View = viewMatrix;
                    }
                    mesh.Draw();
                }
            }
        }


    }
}
